﻿# coding: utf-8

import LogicGrid
import Engine
import Camera
import Math
import Painter

from InputHandler import CInputHandler

class CLogicGridEditor(CInputHandler):
	def __init__(self):
		self._handle = 0
		self._width = 0
		self._height = 0
		self._gridSize = 0
		self._leftButtonDown = False   
		self._currentGridX = -1
		self._currentGridY = -1
		self._visable = False
		self._brushSize = 0
		self._brushValue = 1
		
	def SetBrushSize(self, size):
		self._brushSize = int(size)
		
	def SetBrushValue(self, value):
		self._brushValue = value
		
	def SaveToFile(self, fileName):
		if self._handle:
			return LogicGrid.SaveToFile(self._handle, fileName)
			
		return False
		
	def CreateGrid(self, width, height, gridSize):
		if self._handle:
			self.Destroy()
			
		self._handle = LogicGrid._New()
		if not LogicGrid.Create(self._handle, width, height, gridSize):
			return False
			
		self._width = width
		self._height = height
		self._gridSize = gridSize
		
		LogicGrid.SetGridColor(self._handle, 0, 0x80ffffff)
		LogicGrid.SetGridColor(self._handle, 1, 0x800000ff)
		return True
		
	def __DrawBrush(self):
		startX = self._currentGridX - self._brushSize if self._currentGridX > self._brushSize else 0
		endX = self._currentGridX + self._brushSize if self._currentGridX + self._brushSize < self._width - 1 else self._width - 1
		startY = self._currentGridY - self._brushSize if self._currentGridY > self._brushSize else 0
		endY = self._currentGridY + self._brushSize if self._currentGridY + self._brushSize < self._height -1 else self._height - 1
		
		for x in xrange(startX, endX + 1):
			for y in xrange(startY, endY + 1):
				Painter.DrawTriangle(
					x * self._gridSize + 2, 1, y * self._gridSize + 2,
					(x + 1) * self._gridSize - 2, 1, y * self._gridSize + 2,
					x * self._gridSize + 2, 1, (y + 1) * self._gridSize - 2,
					0, 0, 0, 0, 0, 0, 0x80ffffff, 0x80ffffff, 0x80ffffff, 0)
				Painter.DrawTriangle(
					(x + 1) * self._gridSize - 2, 1, y * self._gridSize + 2,
					x * self._gridSize + 2, 1, (y + 1) * self._gridSize - 2,
					(x + 1) * self._gridSize - 2, 1, (y + 1) * self._gridSize - 2,
					0, 0, 0, 0, 0, 0, 0x80ffffff, 0x80ffffff, 0x80ffffff, 0)					
		
	def Render(self):
		if not self.IsEditing():
			return
			
		width, height = PANEL_EDITOR_SCENE.GetSizeTuple()
		x0, y0, z0, nx, ny, nz = Camera.GetPickRay(Engine.GetCurrentCamera(), width / 2, height / 2, width, height)
		ix, iy, iz = Math.PlaneIntersectLine(0, 1, 0, 0, x0, y0, z0, x0 + nx, y0 + ny, z0 + nz)
		tx = int(ix / self._gridSize) if ix > 0 else 0
		ty = int(iz / self._gridSize) if iz > 0 else 0
		LogicGrid.Render(self._handle, tx, ty, 20)
		
		self.__DrawBrush()
		
		Painter.DrawLine3D(0, 0, 0, self._gridSize * self._width, 0, 0, 0xffffffff, 0xffffffff);
		Painter.DrawLine3D(0, 0, 0, 0, 0, self._gridSize * self._height, 0xffffffff, 0xffffffff);
		Painter.DrawLine3D(self._gridSize * self._width, 0, 0, self._gridSize * self._width, 0, self._gridSize * self._height, 0xffffffff, 0xffffffff);
		Painter.DrawLine3D(0, 0, self._gridSize * self._height, self._gridSize * self._width, 0, self._gridSize * self._height, 0xffffffff, 0xffffffff);
		Painter.Flush()
		
	def Destroy(self):
		LogicGrid._Delete(self._handle)
		self._handle = 0

	def IsEditing(self):
		return self._handle and self._visable
	
	def OnMouseLeftDown(self, e):            
		self._leftButtonDown = True
		x, y = e.GetPosition()
		self.DoPaint()
	    
	def OnMouseLeftUp(self, e):
		self._leftButtonDown = False
	            
	def OnMouseMotion(self, e):
		if not self.IsEditing():
			return
		
		x, y = e.GetPosition()
		width, height = PANEL_EDITOR_SCENE.GetSizeTuple()
		x0, y0, z0, nx, ny, nz = Camera.GetPickRay(Engine.GetCurrentCamera(), x, y, width, height)
		ix, iy, iz = Math.PlaneIntersectLine(0, 1, 0, 0, x0, y0, z0, x0 + nx, y0 + ny, z0 + nz)
		self._currentGridX = int(ix / self._gridSize) if ix > 0 else 0
		self._currentGridY = int(iz / self._gridSize) if iz > 0 else 0
		if self._leftButtonDown:
			self.DoPaint()
				
	def DoPaint(self):
		LogicGrid.Paint(self._handle, self._currentGridX, self._currentGridY, self._brushSize, self._brushValue)
		
	def SetVisable(self, v):
		self._visable = v

__builtins__['LOGIC_GRID_EDITOR'] = CLogicGridEditor()
